package cn.rongdao.jrkabao.ui.takephoto.uicomponent;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;

import cn.rongdao.jrkabao.R;


/**
 * Created by jiangyongxing on 2017/6/23.
 * 描述：
 */

public class ThreeTakePhotoComponent extends LinearLayout {

    private TakePhotoComponent mTakePhotoComponentPositive;
    private TakePhotoComponent mTakePhotoComponentNegative;
    private TakePhotoComponent mTakePhotoComponentHandheld;
    private ImageViewOnclickListener imageViewOnclickListener;
    private SparseArray<String> tempPicUpLoadList = new SparseArray<>();

    public ThreeTakePhotoComponent(Context context) {
        super(context);
    }

    public ThreeTakePhotoComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ThreeTakePhotoComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface ImageViewOnclickListener {
        void onPositivePicClickListener(View view);

        void onNegativePicClickListener(View view);

        void onHandheldPicClickListener(View view);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTakePhotoComponentPositive = findViewById(R.id.lcttp_id_card_positive_photo);
        mTakePhotoComponentNegative = findViewById(R.id.lcttp_id_card_negative_photo);
        mTakePhotoComponentHandheld = findViewById(R.id.lcttp_handheld_id_card);

        init();
    }

    private void init() {
        mTakePhotoComponentPositive.setNameAndDesc("身份证正面", "请尽量避免反光");
        mTakePhotoComponentNegative.setNameAndDesc("身份证反面", "请尽量避免反光");
        mTakePhotoComponentHandheld.setNameAndDesc("手持身份证照", "请尽量使身份证清晰");
        setDefaultPositivePic();
        setDefaultNegativePic();
        setDefaultHandheldPic();

        initListener();
    }

    private void initListener() {
        mTakePhotoComponentPositive.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageViewOnclickListener != null)
                    imageViewOnclickListener.onPositivePicClickListener(v);
            }
        });
        mTakePhotoComponentNegative.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageViewOnclickListener != null)
                    imageViewOnclickListener.onNegativePicClickListener(v);
            }
        });
        mTakePhotoComponentHandheld.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageViewOnclickListener != null)
                    imageViewOnclickListener.onHandheldPicClickListener(v);
            }
        });

    }

    public void setImageViewOnclickListener(ImageViewOnclickListener imageViewOnclickListener) {
        this.imageViewOnclickListener = imageViewOnclickListener;
    }

    public void setDefaultHandheldPic() {
        mTakePhotoComponentHandheld.setPhoto2IV(R.drawable.indentification_card_upload_hand_in);
    }

    public void setDefaultNegativePic() {
        mTakePhotoComponentNegative.setPhoto2IV(R.drawable.indentification_card_upload_opposite);
    }

    public void setDefaultPositivePic() {
        mTakePhotoComponentPositive.setPhoto2IV(R.drawable.indentification_card_upload_front);
    }

    public void setHandheldPic(int resId) {
        mTakePhotoComponentHandheld.setPhoto2IV(resId);
    }

    public void setNegativePic(int resId) {
        mTakePhotoComponentNegative.setPhoto2IV(resId);
    }

    public void setPositivePic(int resId) {
        mTakePhotoComponentPositive.setPhoto2IV(resId);
    }

    public void setHandheldPic(String imageViewPath) {
        setHandheldPic(imageViewPath, 0);
    }

    public void setNegativePic(String imageViewPath) {
        setNegativePic(imageViewPath, 0);
    }

    public void setPositivePic(String imageViewPath) {
        setPositivePic(imageViewPath, 0);
    }

    public void setPositivePic(Bitmap bm) {
        mTakePhotoComponentPositive.setPhoto2IV(bm);
    }

    public void setNegativePic(Bitmap bm) {
        mTakePhotoComponentNegative.setPhoto2IV(bm);
    }

    public void setHandheldPic(String imageViewPath, float rotate) {
        if (TextUtils.isEmpty(imageViewPath))
            return;
        tempPicUpLoadList.put(R.id.lcttp_id_card_positive_photo, imageViewPath);
        mTakePhotoComponentHandheld.setPhoto2IV(imageViewPath, rotate);
    }

    public void setNegativePic(String imageViewPath, float rotate) {
        if (TextUtils.isEmpty(imageViewPath))
            return;
        tempPicUpLoadList.put(R.id.lcttp_id_card_negative_photo, imageViewPath);
        mTakePhotoComponentNegative.setPhoto2IV(imageViewPath, rotate);
    }

    public void setPositivePic(String imageViewPath, float rotate) {
        if (TextUtils.isEmpty(imageViewPath))
            return;
        tempPicUpLoadList.put(R.id.lcttp_handheld_id_card, imageViewPath);
        mTakePhotoComponentPositive.setPhoto2IV(imageViewPath, rotate);
    }


    public void setPositivePiucVisiable(int visiable) {
        mTakePhotoComponentPositive.setVisibility(visiable);
    }

    public void setNegativePiucVisiable(int visiable) {
        mTakePhotoComponentNegative.setVisibility(visiable);
    }

    public void setHandheldPiucVisiable(int visiable) {
        mTakePhotoComponentHandheld.setVisibility(visiable);
    }

    public void removePositivePic() {
        setDefaultPositivePic();
        tempPicUpLoadList.remove(R.id.lcttp_id_card_positive_photo);
    }
}